package com.cloudcc.mobile.view.main.fragment;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;
import java.util.ListIterator;

import android.content.Intent;
import android.text.TextUtils;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.GridView;
import android.widget.ImageView;
import butterknife.OnClick;

import com.cloudcc.cloudframe.bus.EventEngine;
import com.cloudcc.cloudframe.bus.MenuToggleEvent;
import com.cloudcc.cloudframe.model.MenuModel;
import com.cloudcc.cloudframe.model.MenuTabModel;
import com.cloudcc.cloudframe.util.ListUtils;
import com.cloudcc.cloudframe.util.StringUtils;
import com.cloudcc.mobile.AppContext;
import com.cloudcc.mobile.R;
import com.cloudcc.mobile.adapter.FunGridAdapter;
import com.cloudcc.mobile.event.refresh.RefreshMainMenuEvent;
import com.cloudcc.mobile.manager.CacheManager;
import com.cloudcc.mobile.manager.MenuManager;
import com.cloudcc.mobile.manager.UrlManager;
import com.cloudcc.mobile.manager.UserManager;
import com.cloudcc.mobile.util.UrlTools;
import com.cloudcc.mobile.view.activity.BusinesscardScanningActivity;
import com.cloudcc.mobile.view.activity.MenuFilterActivity;
import com.cloudcc.mobile.view.activity.TaskManagementActivty;
import com.cloudcc.mobile.view.attendance.AttendanceMainActivity;
import com.cloudcc.mobile.view.base.BaseFragment;
import com.cloudcc.mobile.view.main.MainCustomActivity;
import com.cloudcc.mobile.view.main.MainUIActivity;
import com.cloudcc.mobile.view.schedule.ScheduleMainActivity;

/**
 * @Description: 主页九宫格样式 的fragment
 * @author wangzhiqiang
 * @date 2015-10-26 上午11:10:02
 * 
 */
public class MainUIFragment extends BaseFragment implements OnItemClickListener {

	private GridView mGridView;

	private FunGridAdapter mAdapter;

	private List<MenuTabModel> menuTabsAll;//所有该显示的

	private List<MenuTabModel> currentShowList;// 当前九宫格显示的

	@Override
	public void init() {
		super.init();
		EventEngine.register(this);
		initView();
		initData();
		initCustom();

	}

	private void initCustom() {
		((ViewGroup) findViewById(R.id.backCustom).getParent())
				.setVisibility(TextUtils.isEmpty(UserManager.getManager()
						.getUser().appMainPage) ? View.GONE : View.VISIBLE);
	}

	private void initView() {
		mGridView = (GridView) findViewById(R.id.gridview);
	}

	public void initData() {
		menuTabsAll = new ArrayList<MenuTabModel>();
		menuTabsAll.clear();

		currentShowList = new ArrayList<MenuTabModel>();
		currentShowList.clear();

		MenuTabModel tabSchedule = new MenuTabModel();
		tabSchedule.id = "task1201";
		tabSchedule.url = "url:task";
		tabSchedule.tab_style = "style:task";
		tabSchedule.tabStyle = "style:task";
		tabSchedule.tabName = "日程事件";
		tabSchedule.tab_name = "日程事件";
		tabSchedule.imageId = "cloudtabtask";
		tabSchedule.objId = "android:task";

		MenuTabModel tabApproval = new MenuTabModel();
		tabApproval.id = "approval1201";
		tabApproval.url = "url:approval";
		tabApproval.tab_style = "style:approval";
		tabApproval.tabStyle = "style:approval";
		tabApproval.tabName = "待审批";
		tabApproval.tab_name = "待审批";
		tabApproval.imageId = "cloudtab_approval";
		tabApproval.objId = "android:approval";

		MenuTabModel tabScanCard = new MenuTabModel();
		tabScanCard.id = "card1201";
		tabScanCard.url = "url:card";
		tabScanCard.tab_style = "style:card";
		tabScanCard.tabStyle = "style:card";
		tabScanCard.tabName = "扫描名片";
		tabScanCard.tab_name = "扫描名片";
		tabScanCard.imageId = "cloudtabscancard";
		tabScanCard.objId = "android:card";
		
/*		MenuTabModel tabKaoqin = new MenuTabModel();
		tabKaoqin.id = "kaoqin1201";
		tabKaoqin.url = "url:kaoqin";
		tabKaoqin.tab_style = "style:kaoqin";
		tabKaoqin.tabStyle = "style:kaoqin";
		tabKaoqin.tabName = "考勤";
		tabKaoqin.tab_name = "考勤";
		tabKaoqin.imageId = "cloudtab_kaoqin";
		tabKaoqin.objId = "android:kaoqin";*/


		menuTabsAll.add(tabSchedule);
		menuTabsAll.add(tabApproval);
		menuTabsAll.add(tabScanCard);
		//menuTabsAll.add(tabKaoqin);
		// 获得ccmenu

		MenuModel menuModel = getMainMenu();

		if (menuModel != null && !ListUtils.isEmpty(menuModel.tab)) {
			menuTabsAll.addAll(menuModel.tab);
		}

		List<MenuTabModel> ingoreMenus = CacheManager.getInstance()
				.getIgnoreMenu();

		ListIterator<MenuTabModel> iterator = menuTabsAll.listIterator();
		while (iterator.hasNext()) {
			MenuTabModel model = iterator.next();
			if ((!ListUtils.isEmpty(ingoreMenus))
					&& ingoreMenus.contains(model)) {
				continue;
			}
			currentShowList.add(model);
		}
		mAdapter = new FunGridAdapter(mContext);
		mAdapter.changeData(currentShowList);
		mGridView.setAdapter(mAdapter);
		mGridView.setOnItemClickListener(this);
	}

	@Override
	public int getLayoutId() {
		return R.layout.main_main;
	}

	@OnClick(R.id.backCustom)
	public void clickEnterCustom() {
		Intent intent = new Intent(mContext, MainCustomActivity.class);
		intent.putExtra("from.type", 1);
		startActivity(intent);
	}

	@OnClick(R.id.menu_filter)
	public void onClickMenuFilter() {
		Intent intent = new Intent(mContext, MenuFilterActivity.class);
		intent.putExtra("sort.filter", (Serializable) menuTabsAll);
		startActivity(intent);
	}

	@OnClick(R.id.chatmes_menubtn)
	public void onClickSlide() {
		EventEngine.post(new MenuToggleEvent(false, false));
	}

	@Override
	public void onItemClick(AdapterView<?> parent, View view, int position,
			long id) {
		AppContext.iscomeMenu = true;

		if (ListUtils.isEmpty(currentShowList)) {
			return;
		}

		MenuTabModel menuTabModel = currentShowList.get(position);
		MenuModel mainMenu = getMainMenu();

		if (StringUtils.isNotBlank(menuTabModel.objId)
				&& menuTabModel.objId.startsWith("android:")) {
			if (menuTabModel.objId.startsWith("android:task")) {// 任务
				Intent intent = new Intent(getActivity(),
						ScheduleMainActivity.class);
				startActivity(intent);
			} else if (menuTabModel.objId.startsWith("android:approval")) {
				Intent intent2 = new Intent(getActivity(),
						TaskManagementActivty.class);
				startActivity(intent2);
			} else if (menuTabModel.objId.startsWith("android:card")) {
				Intent intent = new Intent(getActivity(),
						BusinesscardScanningActivity.class);
				startActivity(intent);
			}else if (menuTabModel.objId.startsWith("android:kaoqin")) {
				Intent intent = new Intent(getActivity(),
						AttendanceMainActivity.class);
				startActivity(intent);
			}
		} else {
			AppContext.indexUrl = UrlManager.getRootUrl()
					+ UrlTools.getModuleUrl(mainMenu.id, mainMenu.label);
			AppContext.urlString = UrlManager.getRootUrl()
					+ UrlTools.getTabsUrl(menuTabModel, mainMenu);
			MenuManager.getInstance().enterMenuDetailOrList(menuTabModel,
					getActivity());
		}
	}

	/**
	 * 刷新
	 * 
	 * @param event
	 */
	public void onEventMainThread(RefreshMainMenuEvent event) {
		initData();
	}

	/**
	 * 获得主页的默认显示menu
	 * 
	 * @return
	 */
	private MenuModel getMainMenu() {
		MenuModel menuModel = ((MainUIActivity) getActivity()).getCCMenuItems();
		return menuModel;
	}

	@Override
	public void onDestroy() {
		EventEngine.uregister(this);
		super.onDestroy();
	}

}
